/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package toster;

//import java.awt.event.EventListener;
import java.util.EventListener;

/**
 * this does the work of the third pass, when it gets a grid event it checks
 * that all the permutations work with the new info and makes any changes it has
 * to.  If it finds out somethings new in makes a new gird event.
 * @author rcluster
 */
public class GridListener implements EventListener{
    private Perms permutations;
    private boolean isHorz;
    private int number;
    private int[][] horizontal;
    private int[][] vertical;
    private EventHorizon EventHorizon;
    /**
     * this is the constructer for the gridlistener
     * @param permutations this is the set of perumtations
     * @param isHorz whether or not the this is a row or col
     * @param number the index either the row col #
     * @param horz the int[][] ths is the horzontal grid
     * @param vert the int[][] ths is the vertical grid
     * @param EventHorizon
     */
    public GridListener(Perms permutations, boolean isHorz, int number,int[][] horz,int[][] vert,EventHorizon EventHorizon) {
        this.permutations = permutations;
        this.isHorz = isHorz;
        this.number = number;
        horizontal=horz;
        vertical=vert;
        this.EventHorizon=EventHorizon;
    }

/**
 *  this is the method that is called when there is a GridEvent
 * @param in
 */
    public void eventDispatched(GridEvent in){
        if(isHorz&&in.getY()==number){
            for(int i=0;i<permutations.size();i++){
                if(permutations.get(i)[in.getX()]!=in.getValue())
                    permutations.remove(i);
            }
            int[] zig=Function.knownInAll(permutations);
            int[] lain=horizontal[in.getY()];
            for(int i=0;i<zig.length;i++){
                if(zig!=lain){
                    EventHorizon.Event(i, number, zig[i]);
                }
            }
        }
        else if(!isHorz&&in.getX()==number){
            for(int i=0;i<permutations.size();i++){
                if(permutations.get(i)[in.getY()]!=in.getValue())
                    permutations.remove(i);
            }
            int[] zig=Function.knownInAll(permutations);
            int[] lain=vertical[in.getX()];
            for(int i=0;i<zig.length;i++){
                if(zig!=lain){
                    EventHorizon.Event(number, i, zig[i]);
                }
            }
        }
    }
}
